/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.action.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.google.code.sapien.interceptor.UserAware;
import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.CategoryAcl;
import com.google.code.sapien.model.Role;
import com.google.code.sapien.model.User;
import com.google.code.sapien.security.CategoryPermissions;
import com.google.code.sapien.security.RequiresAdministrator;
import com.google.code.sapien.security.RolePermissions;
import com.google.code.sapien.service.CategoryAclService;
import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Admin Category Access Control List Action, used to administer category permissions.
 * @author Adam
 * @version $Id: CategoryAclAction.java 30 2010-01-10 19:05:55Z a.ruggles $
 * 
 * Created on Mar 18, 2009 at 12:38:08 PM 
 */
public class CategoryAclAction extends ActionSupport implements UserAware {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -4453172094987830562L;

	/**
	 * The category acl.
	 */
	private CategoryAcl acl;

	/**
	 * A list of ACLs assigned to this role.
	 */
	private List<CategoryAcl> categoryAcls = new ArrayList<CategoryAcl>();

	/**
	 * The category ACL service.
	 */
	private final CategoryAclService categoryAclService;

	/**
	 * The category id.
	 */
	private Long categoryId;

	/**
	 * The Category ACL id.
	 */
	private Long id;

	/**
	 * The logged in user.
	 */
	private User currentUser;

	/**
	 * Category permissions, used to create or update permissions.
	 */
	private CategoryPermissions permissions = new CategoryPermissions();

	/**
	 * The role id.
	 */
	private Long roleId;

	/**
	 * Constructs a category ACL action.
	 * @param categoryAclService The category ACL service.
	 */
	@Inject
	public CategoryAclAction(final CategoryAclService categoryAclService) {
		super();
		this.categoryAclService = categoryAclService;
	}

	/**
	 * Creates a category ACL.
	 * @return SUCCESS.
	 */
	@Action(value = "create-category-acl")
	@RequiresAdministrator
	public String create() {
		acl = new CategoryAcl();
		acl.getRole().setId(roleId);
		acl.getCategory().setId(categoryId);
		acl.setPermissions(permissions);
		categoryAclService.add(acl, currentUser);
		addActionMessage(this.getText("create-category-acl.success", "The permissions were created successfully."));
		return SUCCESS;
	}

	/**
	 * Returns acl.
	 * @return the acl.
	 */
	public CategoryAcl getAcl() {
		return acl;
	}

	/**
	 * Returns categoryAcls.
	 * @return the categoryAcls.
	 */
	public List<CategoryAcl> getCategoryAcls() {
		return categoryAcls;
	}

	/**
	 * Returns categoryId.
	 * @return the categoryId.
	 */
	public Long getCategoryId() {
		return categoryId;
	}

	/**
	 * Returns id.
	 * @return the id.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * Returns permissions.
	 * @return the permissions.
	 */
	public CategoryPermissions getPermissions() {
		return permissions;
	}

	/**
	 * Returns roleId.
	 * @return the roleId.
	 */
	public Long getRoleId() {
		return roleId;
	}

	/**
	 * Returns a JSON array of category ACLs for a given category.
	 * @return SUCCESS.
	 */
	@Action(value = "json-category-category-acl", results = {
			@Result(name = "success", location = "json-category-acl.jsp")
	})
	@RequiresAdministrator
	public String jsonCategory() {
		if (categoryId != null) {
			final Category category = new Category();
			category.setId(categoryId);
			categoryAcls = categoryAclService.list(category);
		}
		return SUCCESS;
	}

	/**
	 * Returns a JSON array of category ACLs for a given role.
	 * @return SUCCESS.
	 */
	@Action(value = "json-role-category-acl", results = {
			@Result(name = "success", location = "json-category-acl.jsp")
	})
	@RequiresAdministrator
	public String jsonRole() {
		if (roleId != null) {
			final Role role = new Role();
			role.setId(roleId);
			categoryAcls = categoryAclService.list(role);
		}
		return SUCCESS;
	}

	/**
	 * Removes the permissions on a category ACL.
	 * @return SUCCESS.
	 */
	@Action(value = "remove-category-acl")
	@RequiresAdministrator
	public String remove() {
		acl = new CategoryAcl();
		acl.setId(id);
		categoryAclService.remove(acl, currentUser);
		addActionMessage(this.getText("remove-category.success", "The permissions were removed successfully."));
		return SUCCESS;
	}

	/**
	 * Sets acl.
	 * @param acl the acl to set.
	 */
	public void setAcl(final CategoryAcl acl) {
		this.acl = acl;
	}

	/**
	 * Sets categoryId.
	 * @param categoryId the categoryId to set.
	 */
	public void setCategoryId(final Long categoryId) {
		this.categoryId = categoryId;
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.interceptor.UserAware#setCurrentUser(User)
	 */
	public void setCurrentUser(final User currentUser) {
		this.currentUser = currentUser;
	}

	/**
	 * Sets permissions.
	 * @param permissions the permissions to set.
	 */
	public void setPermissions(final CategoryPermissions permissions) {
		this.permissions = permissions;
	}

	/**
	 * Sets roleId.
	 * @param roleId the roleId to set.
	 */
	public void setRoleId(final Long roleId) {
		this.roleId = roleId;
	}

	/**
	 * Updates the permissions on a category ACL.
	 * @return SUCCESS.
	 */
	@Action(value = "update-category-acl")
	@RequiresAdministrator
	public String update() {
		acl = new CategoryAcl();
		acl.setId(id);
		acl.setPermissions(permissions);
		acl = categoryAclService.update(acl, currentUser);
		addActionMessage(this.getText("update-category-acl.success", "Your updates were saved successfully."));
		return SUCCESS;
	}

}
